<!--添加责任人>联系人 -->
<template>
    <div class="page maintainOwner-page" data-page="maintainOwner" style="z-index: 2">
        <div class="link-page-nav">
            <a href="#" class="link" @click=$root.backToTab()>
                <i class="iconfont icon-zuo"></i>
            </a>
        </div>
        <div class="link-page-top">
            <div class="link-page-title">联系人</div>
        </div>
        <div data-infinite-distance="50" class="page-content infinite-scroll-content ptr-content pdbottom100 rdsp-org-list">
            <div class="list media-list" style="margin-top: 0;">
                <ul id="dataOwnerList_{{getNewId}}" class="sort_box"></ul>
            </div>
        </div>
        <div class="initials" id="initials_{{getNewId}}"><ul></ul></div>
        <div class="toolbar tabbar tabbar-labels toolbar-bottom-md" class="toolbar-bottom">
    		<div class="toolbar-inner">
                  <a href="#" class="tab-link selectTip" style="display: inline-block;padding-left: 20px;">
                     <span class="selectNum">已选择 <i id="selectOwnerNum_{{getNewId}}">0</i> 个</span>
                  </a>
                  <a href="#" @click="updateMaitainAccount" class="tab-link">
                     <div class="addOwnerBtn">确定</div>
                  </a>
            </div>
        </div>
    </div>
</template>

<script>
return {
	mounted() {
		var self = this;
		var app = self.$app;
		self.orgIds = this.$route.params.orgIds;
		self.taskIds = this.$route.params.taskIds;
		self.jumpFromPage = this.$route.params.pageName;
        self.hasChecked = (this.$route.params.personObj=="-1")?"":this.$route.params.personObj;  //解决路由传空字符串问题;
        self.accounts = self.hasChecked;  //解决路由空字符串问题
    },
    data: function() {
        return {
            getNewId: (new Date()).getTime(),
        	accounts:"",
            hasChecked:"",
            submit:false,
        }
    },
    methods: {
    	getTaskOwner:function(orgId){
            var self = this;
            self.userObj={};
            self.touBType = [];
            common.loading(1);
    		$("#dataOwnerList_" + self.getNewId).empty();
            var queryData = {
                 userName:userInfor.accountName,
                 deviceId:appKeyObj.deviceId,
                 orgIds:self.orgIds,
                 roleIds:maitenceUserId.toString(),  //维保方角色id
            }
            Dao.getHaveOrgUserListNotInRole(queryData,function(data) {
                if (data!=null&&data.length != 0) {
                     for (var i = 0;i < data.length > 0;i++){
                         //var name= data[i].name;
                         $("#dataOwnerList_" + self.getNewId).append(`
                             <li class="sort_list" data-address="`+common.transNullundefinedToline(data[i].name)+`">
                         		<div class="item-inner checkboxDev" style="padding-top: 0px;padding-bottom: 1px;margin-top: 12px;padding-left:0;padding-right:0">
                 					<label class="checkbox" for="check_`+i+`" style="height: 48px;text-align: center;display: flex;align-items: center;justify-content: space-around;">
                                        <input type="checkbox" value="`+common.transNullundefinedToline(data[i].accountName)+`"/ id="check_`+i+`">
                                        <i class="icon-checkbox"></i>
                                    </label>
                 				</div>
                 				<div class="item-inner ownerInfo">
	                 				<div class="item-subtitle decter-mesage">
		                 				<div class="ownerName num_name">`+common.transNullundefinedToline(data[i].name)+`</div>
	                 					<div class="postName" > `+common.transNullundefinedToline(data[i].postName)+`</div>
             						</div>
             						<div class="item-subtitle decter-mesage ownerPhone">`+common.transNullundefinedToline(data[i].phone)+`</div>
             					</div>			
                    		</li>
                         `);
                    }
                    if(data.length>19){
                        jQuery("#initials_" + self.getNewId).show()
                        stratSort()
                    }else{
                        jQuery("#initials_" + self.getNewId).hide()
                    }
                    
                    setTimeout(()=>{
                        self.getSelectCheckBox();
                        if(self.hasChecked){
                            let arr = self.hasChecked.split(",");
                            for(let i=0; i<arr.length; i++ ){
                                if(arr[i]){
                                    jQuery("#dataOwnerList_" + self.getNewId+ " .checkbox input[value="+arr[i]+"]").attr("checked",true);
                                }
                            }
                        }
                        common.loading(0);
                        var chks=jQuery("#dataOwnerList_" + self.getNewId + " .checkbox input:checked").length;
                     	jQuery("#selectOwnerNum_" + self.getNewId).html(chks);
                    },1000);
                }else{
                    common.loading(0);
                }
             });
         },
         //单个复选框选中事件
         getSelectCheckBox:function(){
        	 var self = this;
        	 jQuery("#dataOwnerList_" + self.getNewId + " .checkbox input[type=checkbox]").off().change(function(e) {
        		self.accounts="";
        		var chks=jQuery("#dataOwnerList_" + self.getNewId + " .checkbox input:checked").length;
             	jQuery("#selectOwnerNum_" + self.getNewId).html(chks);
             	var checkOwner = jQuery("#dataOwnerList_" + self.getNewId + " .checkbox input:checked");
                for ( var i = 0; i <checkOwner.length; i++){
                	var ownerAccount = jQuery(checkOwner[i]).val();
                 	if(i<checkOwner.length-1){
                 		ownerAccount=ownerAccount+",";
                 	}
                 	self.accounts+=ownerAccount;
                }
         	});
         },
         //更新/添加责任人
         updateMaitainAccount:function(){
             var self = this;
        	 if(self.accounts==""){
     			app.methods.showToastCenter('请选择联系人');
     			return;
     		 }
        	 //避免重复提交
        	 if(self.submit){
        		return;
        	 }
        	 self.submit = true;
        	 
             var queryData = {
                  accounts:self.accounts,
                  taskId:self.taskIds,
             }
             Dao.updateMaitainAccount(queryData,function(data) {
    	 		app.methods.showToastCenter('操作成功');
    	 		setTimeout(function () {
                    app.methods.backToTab();
	    	 		//保养列表页面添加责任人
	    	 		if(self.jumpFromPage=="maintain"){
	    	 			app.view.current.el.f7View.router.currentPageEl.f7Component.selectCurrentType(1); 
	    	 		}
	                //维保巡查列表
	                else if(self.jumpFromPage=="maintainInspect"){
	                    app.view.current.el.f7View.router.currentPageEl.f7Component.updateListView(1); 
	                }
	                //单点测试列表
	                else if(self.jumpFromPage=="singleTest"){
	                    app.view.current.el.f7View.router.currentPageEl.f7Component.selectCurrentType(1); 
	                }
	                //联动测试列表
	                else if(self.jumpFromPage=="linkTest"){
	                    app.view.current.el.f7View.router.currentPageEl.f7Component.selectCurrentType(1); 
	                }
	    	 		//作业页面添加责任人
	                else if(self.jumpFromPage=="allBUild"){
	    	 			app.view.current.el.f7View.router.currentPageEl.f7Component.htmlPage(); 
	    	 		}
	    	 		//作业页面添加责任人
	                else if(self.jumpFromPage=="remainBUild"){
	    	 			app.view.current.el.f7View.router.currentPageEl.f7Component.htmlPage(); 
	    	 		}
	                //维保巡查列表
	                else if(self.jumpFromPage=="maintainInspect"){
	                    app.view.current.el.f7View.router.currentPageEl.f7Component.finishAddOwner(); 
	                }
	                //保养详情
	                //维保巡查详情
	                //单点测试详情
	                //联动测试详情
	                else if(self.jumpFromPage=="maintainDetail" || self.jumpFromPage=="maintainInspectDetail" || 
	                    self.jumpFromPage=="singleTestDetail" || self.jumpFromPage== "linkTestDetail"){
	                    app.view.current.el.f7View.router.currentPageEl.f7Component.getInfoData(true); 
	                }
    	 		}, 1000);	
             });
         },
    },
    on: {
        pageInit: function(e, page) {
            var self = this;
            var $ = self.$;
            self.getTaskOwner();
        },
    }
}
</script>